
// 云函数调用
export const Mycloud = (url, obj, method) => {

  const { cloud } = getApp();
  console.log(cloud)
  return cloud.function.invoke(url, obj, method);
}

// 字符串转对象
export function strToObj(res) {
  for (let key in res) {
    try {
      res[key] = JSON.parse(res[key])
    } catch (error) {
      res[key]
    }
  }
  return res
} 

// 隐藏姓名
export function hideName(name) {
  if (!name) {
    return `X***5`
  }
  let first = name.substr(0, 1);
  let end = name.substr(name.length - 1, name.length);
  return `${first}***${end}`;
}

// 将时间转化为 8.10 这种格式
export function setTime(time) {
  let timer = new Date(parseInt(time))
  return `${timer.getMonth() + 1}.${timer.getDate()}`;
}

// 获取服务器时间
export function getServerTime() {
  return new Promise((res) => {
    my.getServerTime({
      success: result => {
        res(result);
      }
    });
  });
}

// 获取随机数
export function Random(min, max) {
  return Math.round(Math.random() * (max - min)) + min;
}

// 获取用户信息
export const userInfo = () => {
  return new Promise((res, rej) => {
    my.authorize({
      scopes: 'scope.userInfo',
      success: (result) => {
        my.getAuthUserInfo({
          success: (userInfo) => {
            res(userInfo)
          }
        });
      },
    });
  })

}

/**
 *  
 * @param {*} obj 传入的类型
 * @param {string}  type 判断是否是该类型
 */
export const toType = (obj, type) => {
  let res = Object.prototype.toString.call(obj);
  if (res == `[object ${type}]`) {
    return true;
  }
  return false

}

// 是否关注店铺
/**
 * 
 * @param {Number} shopId 店铺sellerID  
 * 返回值 {id: "123456", isFavor: true} 
 * 
 */
export const isFollowStore = (shopId) => {
  return new Promise((resolve, reject) => {
    my.tb.checkShopFavoredStatus({
      id: shopId,
      success: (res) => {
        resolve(res);
      },
      fail: (res) => {
        reject(res)
      }

    })

  })
}

// 关注店铺
export const followStore = (shopId) => {
  return new Promise((resolve, reject) => {

    my.tb.favorShop({
      id: shopId,
      success: (res) => {
        resolve(res, 'success')
      },
      fail: (res) => {
        reject(res, 'fail')
      }
    })

  }).catch(err => {
    return err
  })
}

// 判断是否关注店铺 没有的话调用关注接口
/**
 * 
 * @param {Number} shopId 店铺ID  
 * @param {function} fun 回调函数  
 * 
 */
export async function followStoreFn(shopId, fun) {
  let res = await isFollowStore(shopId);
  if (res.isFavor == true && res) {
 
    return 
  };

  let followState = await followStore(shopId)

  if (followState.error == 11) {
    // followStoreFn(shopId);
    console.log("拒绝关注")
  }else{
    if (fun) {
      fun()
    }
  }

}

// 倒计时
/**
 * 
*  @param {Number} startTime 开始时间戳 
 * @param {Number} endTime 结束时间戳 
 * 
 */
export function durationTime(startTime,endTime) {
   return new Promise((res) => {
      //  setInterval(()=>{

        // my.getServerTime({
        // success:(myresult)=>{
        //   let serverTime = myresult.time
       
      let durationTime = parseInt(endTime)-parseInt(startTime)
      if(durationTime<=0){
        // clearInterval(durTime)
        return
      }
      // console.log("serverTime:"+serverTime)
      // console.log("durationTime:"+durationTime)
    const day = parseInt(durationTime / 1000 / 3600 / 24);
    const hour = parseInt((durationTime / 1000 / 3600) % 24);
    const min = parseInt((durationTime / 1000 / 60) % 60);
    const sec = parseInt((durationTime / 1000) % 60);
      // console.log("天："+day+"时："+hour+"分："+min+"秒："+sec)

      let result={day,hour,min,sec}
      // console.log("public.js:"+JSON.stringify(result))
        res(result)
          // }
        })
      // },1000)
    // });
  }
